package com.demomaster.quickcompat.drawable;

import android.content.res.ColorStateList;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class QuickRoundDrawable extends StateListDrawable implements QuickDrawable{

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private RectF boundsF = new RectF();
    private RectF boundsF2 = new RectF();
    private Path path = new Path();
    //private float radius;
    private Drawable wrapped;
    private Drawable backgroundDrawable;
    public QuickRoundDrawable(Drawable wrapped, float[] radii) {
        this.wrapped = wrapped;
        this.mRadii = radii;
    }
    int paddingLeft = 0;
    int paddingTop = 0;
    int paddingRight = 0;
    int paddingBottom = 0;
    public void setPadding(int paddingLeft, int paddingTop, int paddingRight, int paddingBottom){
        this.paddingLeft = paddingLeft;
        this.paddingTop = paddingTop;
        this.paddingRight = paddingRight;
        this.paddingBottom = paddingBottom;
        onBoundsChange(getBounds());
    }
    public QuickRoundDrawable(Drawable wrapped, float radius) {
        this.wrapped = wrapped;
        //this.radius = radius;
        this.mRadii = new float[]{radius,  radius, radius, radius, radius, radius, radius, radius};
    }
    float[] mRadii;
    float mRadius = 0;
    public void setCornerRadii(float[] radii) {
        if(wrapped instanceof GradientDrawable){
            ((GradientDrawable) wrapped).setCornerRadii(radii);
        }
        this.mRadii = radii;
        invalidateSelf();
    }

    @Override
    public void setCornerRadius(float radius) {
        if(wrapped instanceof GradientDrawable){
            ((GradientDrawable) wrapped).setCornerRadii(mRadii);
        }
        this.mRadius = radius;
        this.mRadii = new float[]{radius,  radius, radius, radius, radius, radius, radius, radius};
        invalidateSelf();
    }

    Paint mStrokePaint;
    ColorStateList mStrokeColors;
    int mStrokeWidth;
    @Override
    public void setStroke(int borderWidth, ColorStateList borderColor) {
        if(borderWidth<0){
            mStrokePaint = null;
            mStrokeColors = borderColor;
            mStrokePaint.setStyle(Paint.Style.STROKE);
            mStrokePaint.setStrokeWidth(borderWidth);
            mStrokeWidth = borderWidth;
        }else {

            mStrokePaint = new Paint();
            mStrokeColors = borderColor;
            mStrokePaint.setStyle(Paint.Style.STROKE);
            mStrokePaint.setStrokeWidth(borderWidth);
            mStrokeWidth = borderWidth;
        }
        invalidateSelf();
    }

    public void setRadius(float radius) {
        //this.radius = radius;
        this.mRadii = new float[]{radius,  radius, radius, radius, radius, radius, radius, radius};
        invalidateSelf();
    }

    public void setBackgroundDrawable(Drawable backgroundDrawable) {
        this.backgroundDrawable = backgroundDrawable;
        invalidateSelf();
    }

    public Drawable getBackgroundDrawable() {
        return backgroundDrawable;
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        canvas.save();
        //RectF boundsF1 = new RectF(boundsF.left+paddingLeft,  boundsF.top+paddingTop, boundsF.right+paddingRight, boundsF.bottom+paddingBottom);
        path.addRoundRect(boundsF, mRadii, Path.Direction.CCW);
        //path.addRoundRect(boundsF, radius, radius, Path.Direction.CCW);
        canvas.clipPath(path);
        if(backgroundDrawable != null){
            backgroundDrawable.draw(canvas);
        }
        path.reset();
        if(wrapped!=null) {
            path.addRoundRect(boundsF2, mRadii, Path.Direction.CCW);
            canvas.clipPath(path);
            //canvas.translate(paddingLeft, paddingTop);
            wrapped.draw(canvas);
        }
        canvas.restore();
        if(mStrokePaint!=null) {
            canvas.drawPath(path, mStrokePaint);
        }

//绘制边框
        if(mStrokeWidth>0) {
            Rect rect = getBounds();
            RectF rectF = new RectF(rect); // 将 Rect 转换为 RectF
            Path path = new Path();
            if (mRadii != null) {
                path.addRoundRect(rectF, mRadii, Path.Direction.CW);
            } else {
                path.addRoundRect(rectF, mRadius, mRadius, Path.Direction.CCW);
            }

            Paint mBorderPaint = new Paint();
            mBorderPaint.setStyle(Paint.Style.STROKE);
            mBorderPaint.setStrokeWidth(mStrokeWidth);
            mBorderPaint.setColor(mStrokeColors.getColorForState(getState(), Color.BLACK));
            canvas.drawPath(path, mBorderPaint);
        }
    }

    @Override
    public void setAlpha(int alpha) {
        if(wrapped!=null) {
            wrapped.setAlpha(alpha);
        }
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        if(wrapped!=null) {
            wrapped.setColorFilter(colorFilter);
        }
    }

    @Override
    public int getOpacity() {
        if(wrapped==null){
            return PixelFormat.OPAQUE;
        }
        return wrapped.getOpacity();
    }

    @Override
    protected void onBoundsChange(@NonNull Rect bounds) {
        super.onBoundsChange(bounds);
        boundsF.set(bounds);
        boundsF2.set(bounds.left+paddingLeft,
                bounds.top+paddingTop,
                bounds.right-paddingRight,
                bounds.bottom-paddingBottom);


        if(wrapped!=null) {
            wrapped.setBounds(bounds);
        }
        if(backgroundDrawable != null){
            backgroundDrawable.setBounds(bounds);
        }
        path.reset();
    }

    @Override
    public boolean setState(@NonNull int[] stateSet) {
        if(backgroundDrawable != null){
            backgroundDrawable.setState(stateSet);
        }
        if(wrapped!=null) {
           return wrapped.setState(stateSet);
        }
        return true;
    }

    @Override
    protected boolean onStateChange(int[] stateSet) {
        boolean changed = false;
        if(wrapped!=null){
            changed = wrapped.setState(stateSet);
        }
        if(backgroundDrawable!=null){
            changed |= backgroundDrawable.setState(stateSet);
        }
        if(changed){
            invalidateSelf();
        }
        return changed;
    }

    @Override
    public boolean isStateful() {
        return (wrapped!=null&&wrapped.isStateful())||(backgroundDrawable!=null&&backgroundDrawable.isStateful());//super.isStateful();
    }

    public Drawable getWrapped() {
        return wrapped;
    }
}
